package lj.api;

import lj.pojo.Grade;
import lj.pojo.Student;

import java.util.ArrayList;

/**
 * @author lj
 * @Description: 功能封装在此类中
 * @date 2020-11-23 10:11
 * @QQ 851477108
 */


public class Function {
    private ArrayList<Student> arrayList;
    public Function(ArrayList<Student> arrayList){
        super();
        this.arrayList = arrayList;
    }

    //根据名字搜索该学生的成绩
    public void show(String name){
        for (Student s : arrayList) {
            if (s.getName().equals(name)){
                ArrayList<Grade> grade = s.getGrade();
                System.out.println("该学生的成绩是：");
                for (Grade g : grade){
                    System.out.println(g);
                }
                break;
            }
        }
        System.out.println("分割线---------------------------");
    }

    //列出某门课程每个学生的总评成绩，及所有学生总评成绩的总评分
    public void showOneSum(String course){
        double sumGrade = 0;
        int num = 0;
        for (Student s : arrayList){
            ArrayList<Grade> grades = s.getGrade();
            for (Grade g : grades){
                if (course.equals(g.getCourse())){
                    num++;
                    sumGrade += g.getSumGrade();
                    System.out.println(s.getName()+"的总评分是"+g.getSumGrade());
                }
            }
        }
        System.out.println("所有学生的总评分是："+sumGrade/num);
        System.out.println("分割线---------------------------");
    }

    //分区段统计某门课程的学生总评成绩，例如60 分以下的学生人数、60分以上的学生人数等
    public void selectGrade(String course){
        int PassGrad = 0;
        int UnPassGrad = 0;
        for (Student s : arrayList){
            ArrayList<Grade> grades = s.getGrade();
            for (Grade g: grades){
                if (course.equals(g.getCourse())){
                    if (g.getSumGrade() < 60){
                        UnPassGrad++;
                        System.out.println("不及格的有："+s.getName()+"-"+g.getCourse()+"-"+g.getSumGrade());
                    }else {
                        PassGrad++;
                        System.out.println("及格的有："+s.getName()+"-"+g.getCourse()+"-"+g.getSumGrade());
                    }
                }
            }
        }
        System.out.println("60分以下的有"+PassGrad+"人");
        System.out.println("60分以上的有"+UnPassGrad+"人");
        System.out.println("分割线---------------------------");
    }

}















